package com.tianqi.ui.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tianqi.ui.bean.WeatherForecast;
import com.tianqi.ui.bean.WeatherInfo;
import com.tianqi.ui.bean.Yesterday;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class Service2 {
    public DefaultListModel<String> query(String city){
        DefaultListModel<String> infos = new DefaultListModel<String>();
        URL url = null;
        try {
            if(city!=null && !"".equals(city)) {
                url = new URL("https://api.iyk0.com/6rtq/?city="+city);
            }else {
                url = new URL("https://api.iyk0.com/6rtq/?city=连云港");
            }
            URLConnection conn = url.openConnection();

            conn.connect();
            InputStream in = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
            String line = null;
            line=reader.readLine();

            // ObjectMapper mapper = new ObjectMapper();
            // Response readValue = mapper.readValue(line, .class);

            ObjectMapper mapper = new ObjectMapper();
            WeatherForecast forecast = mapper.readValue(line,WeatherForecast.class);
            String cityName =forecast.getData().getCity();
            String ganmao = forecast.getData().getGanmao();
            String wendu = forecast.getData().getWendu();

            //Yesterday yesterday = forecast.getData().getYesterday();
            infos.addElement("所在城市："+cityName);
            infos.addElement("温馨提醒："+ganmao);
            infos.addElement("温度："+wendu+"°C");

            // List<WeatherInfo> weathers = forecast.getData().getForecast();
            // for(WeatherInfo w:weathers){
            //     infos.addElement("--------------------");
            //     infos.addElement("日期："+w.getDate());
            //     infos.addElement("天气："+w.getType());
            //     infos.addElement("最高温度："+w.getHigh());
            //     infos.addElement("最低温度："+w.getLow());
            //     infos.addElement("风向："+w.getFengxiang());
            //     infos.addElement("风力："+w.getFengli());
            // }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return infos;
    };

}
